package ctu.edu.vn.musicmanager.gui;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.jms.MessageListener;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Properties;

import javax.swing.JLabel;
import java.awt.Color;

public class FlashScreen extends JFrame {

	private JPanel contentPane;
	private JTextField txtHostIP;

	MessageListener mf;
	boolean connecting;
	boolean success;
	JFrame self;
	private class Connector extends Thread implements Runnable {
		public Connector() {
			super(new Thread());
		}
		@Override
		public void run() {
			connecting = true;
			boolean res = Connect();
			if (res) {
				success = true;
			}
			connecting = false;
			this.interrupt();
		}
	}
	private class Editor extends Thread implements Runnable {
		JButton btn;
		JTextField txt;
		public Editor(JButton btn, JTextField txt) {
			super(new Thread());
			this.btn = btn;
			this.txt = txt;
		}
		@Override
		public void run() {
			int k = 0;
			btn.setEnabled(false);
			txt.setEditable(false);
			while (connecting) {
				if (k == 0)
					btn.setText("Đang kết nối");
				if (k == 1)
					btn.setText("Đang kết nối.");
				if (k == 2)
					btn.setText("Đang kết nối..");
				if (k == 3)
					btn.setText("Đang kết nối...");
				k = (k + 1) % 4;
				try {
					this.sleep(1000);
				} catch (InterruptedException e) {
				}
			}
			if (success) {
				MusicManager.host = txt.getText();
				self.setVisible(false);
			} else {
				btn.setEnabled(true);
				btn.setText("Kết nối");
				txt.setEditable(true);
				JOptionPane.showMessageDialog(self, "Kết nối thất bại!");
			}
			this.interrupt();
		}
	}
	public FlashScreen() {
		success = false;
		connecting = false;
		self = this;
		setUndecorated(true);
		getRootPane().setWindowDecorationStyle(JRootPane.NONE);
		setBounds(100, 100, 500, 500);
		contentPane = new JPanel();
		contentPane.setPreferredSize(new Dimension(500, 500));
		contentPane.setMaximumSize(new Dimension(500, 500));
		contentPane.setBorder(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtHostIP = new JTextField();
		txtHostIP.setForeground(Color.BLACK);
		txtHostIP.setPreferredSize(new Dimension(6, 32));
		txtHostIP.setText("127.0.0.1");
		txtHostIP.setHorizontalAlignment(SwingConstants.LEFT);
		txtHostIP.setToolTipText("IP của Server");
		txtHostIP.setBounds(10, 454, 150, 34);
		contentPane.add(txtHostIP);
		txtHostIP.setColumns(10);
		
		final JButton btnConnect = new JButton("Kết nối");
		btnConnect.setPreferredSize(new Dimension(65,  32));
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Connector con = new Connector();
				con.start();
				Editor edt = new Editor(btnConnect, txtHostIP);
				edt.start();
			}
		});
		btnConnect.setBounds(214, 453, 117, 36);
		contentPane.add(btnConnect);
		
		JButton btnExit = new JButton("Thoát");
		btnExit.setIcon(new ImageIcon("./icons/exit.png"));
		btnExit.setPreferredSize(new Dimension(65,  32));
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (JOptionPane.showConfirmDialog(self, "Bạn thật sự muốn thoát?", "Chọn lựa", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
					System.exit(0);
			}
		});
		btnExit.setBounds(341, 453, 105, 36);
		contentPane.add(btnExit);
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((dimension.getWidth() - getWidth()) / 2);
		int y = (int) ((dimension.getHeight() - getHeight()) / 2);
		setLocation(x, y);
		
		
		
		JLabel background = new JLabel("");
		background.setMaximumSize(new Dimension(500, 500));
		background.setMinimumSize(new Dimension(500, 500));
		background.setBounds(0, 0, 500, 500);
		background.setIcon(new ImageIcon("./icons/background.png"));
		contentPane.add(background);
		
		Connector con = new Connector();
		Editor edt = new Editor(btnConnect, txtHostIP);
		con.start();
		edt.start();
	}
	public void setConnect(MessageListener mf) {
		this.mf = mf;
	}
	boolean Connect() {
		String host = txtHostIP.getText();
		//System.out.println(host);
		try {
			Context context = getContext(host);
			TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context
					.lookup("ConnectionFactory");
			MusicManager.topicSender = (Topic) context.lookup("topic/JMSServerReciever");
			TopicConnection topicConnection = (TopicConnection) topicConnectionFactory
					.createTopicConnection();
			MusicManager.topicSession = topicConnection.createTopicSession(false,
					TopicSession.AUTO_ACKNOWLEDGE);
			MusicManager.topicPublisher = MusicManager.topicSession.createPublisher(MusicManager.topicSender);
			topicConnection.start();
			MusicManager.topicSession.createSubscriber(
					(Topic) context.lookup("topic/JMSServerSender"))
					.setMessageListener(mf);
			return true;
		} catch (Exception e1) {
			return false;
		}
		
	}
	public Context getContext(String cur_host) throws NamingException {
		Properties props = new Properties();
		props.setProperty("java.naming.factory.initial",
				"org.jnp.interfaces.NamingContextFactory");
		props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
		props.setProperty("java.naming.provider.url", cur_host + ":1099");
		return new InitialContext(props);
	}
}
